<template>
  <div class="pdf-wrap scrollbar-style">
      <vue-pdf-embed :source="state.source" v-if="state.pdfShow" :style="scaleFun" class="vue-pdf-embed" ref="vuePdfRef" :page="state.pageNum"/>
  </div>
  <div class="page-tool">
      <div class="page-tool-item" @click="lastPage">上一页</div>
      <div class="page-tool-item">{{state.pageNum}}/{{state.numPages}}</div>
      <div class="page-tool-item" @click="nextPage">下一页</div>
      <div class="page-tool-item" @click="pageZoomOut">放大</div>
      <div class="page-tool-item" @click="pageZoomIn">缩小</div>
      <div class="page-tool-item" @click="PDFPrint">下载</div>
  </div>
</template>
<script setup>
import { reactive, onMounted, computed } from "vue";
import VuePdfEmbed from "vue-pdf-embed";
// import { createLoadingTask } from "vue3-pdfjs/esm";  // 使用这个插件后有可能预览不了
// import { debounce } from "@/utils"; // 防抖方法

function debounce(){}

const props = defineProps({
  url: {
      type: String,
      required: true
  }
})
const scaleFun = computed(() => `transform:scale(${state.scale});margin-top:2%`)
const state = reactive({
  source: props.url, // 预览pdf文件地址
  pageNum: 1, // 当前页面
  scale: 1, // 缩放比例
  numPages: 0, // 总页数
  pdfShow: true, // pdf是否显示
});
const vuePdfRef =ref(null)
// 上一页
function lastPage() {
  if (state.pageNum > 1) {
      state.pageNum -= 1;
  }
}
// 下一页
function nextPage() {
  if (state.pageNum < state.numPages) {
      state.pageNum += 1;
  }
}
// 放大
function pageZoomOut() {
  if (state.scale < 2) {
      state.scale += 0.1;
  }
}
// 缩小
function pageZoomIn() {
  if (state.scale > 1) {
      state.scale -= 0.1;
  }
}
// 打印/下载
function PDFPrint() {
  vuePdfRef.value.print(300,'合同',true); // 打印：参数1为分辨率 参数2为文件名称 参数3为是否打印所有页面
  // vuePdfRef.value.download('合同'); // 下载：参数为文件名称。我这里会报错可能是有方法冲突了
}
// 滚轮滚动
const handleScroll = debounce((event) => {
  event.preventDefault(); // 阻止默认的滚动行为,这里不知为什么不起效,所以直接使用@wheel.prevent
  // 判断是否是ctrl+滚轮
  if (event.ctrlKey) { // 是ctrl+滚轮就放大缩小页面
      if (event.deltaY > 0) {
          pageZoomOut();
      } else if (event.deltaY < 0) {
          pageZoomIn();
      }
  } else { // 不是就滑动滚轮来翻页
      event.wheelDeltaY < 0 ? nextPage() : lastPage();
  }
}, 50)

onMounted(() => {
//   const loadingTask = createLoadingTask(state.source);
//  loadingTask.promise.then((pdf) => {
//      state.numPages = pdf.numPages;
//  });
});
</script>
<style lang="scss" scoped>
.pdf-preview {
  position: relative;
  height: 100vh;
  padding: 20px 0;
  box-sizing: border-box;
  background: rgb(66, 66, 66);
}
.vue-pdf-embed {
  text-align: center;
  width: 515px;
  border: 1px solid #e5e5e5;
  margin: 0 auto;
  box-sizing: border-box;
}
.pdf-wrap{
  overflow-y:auto ;
}

.page-tool {
  position: absolute;
  bottom: 35px;
  padding-left: 15px;
  padding-right: 15px;
  display: flex;
  align-items: center;
  background: rgba(3, 67, 109, 0.075);
  color: rgba(2, 1, 41, 0.507);
  border-radius: 19px;
  z-index: 100;
  cursor: pointer;
  margin-left: 52%;
  transform: translateX(-50%);
}
.page-tool-item {
  padding: 8px 15px;
  padding-left: 10px;
  cursor: pointer;
}
</style>